<?php
/**
 * Joomla! 1.5 component eventcalendar
 *
 * @version $Id: eventcalendar.php 2010-10-01 08:17:29 svn $
 * @author Pasi Lampinen
 * @package Joomla
 * @subpackage eventcalendar
 * @license GNU/GPL
 *
 * Tapahtumakalenteri Joomlaan geolokaatiolla
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
require_once JPATH_COMPONENT.DS.'helpers'.DS.'functions.check.php';
require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_eventcalendar'.DS.'helpers'.DS.'database.php';

/**
 * eventcalendar Component georss Model
 *
 * @author      notwebdesign
 * @package		Joomla
 * @subpackage	eventcalendar
 * @since 1.5
 */





class TableTapahtuma extends JTable
{
   
    var $id = null;
    var $tapahtuma_luokka_id = null;
    var $alkuaika;	
    var $loppuaika= null;
    var $nimi = null;
    var $kuva= null;
    var $kuvaus= null;
    var $toistuminen= null;
    var $tila= null;
    var $paikka_teksti= null;
    var $position_id= null;
    var $yhteys_henkilo_nimi= null;
    var $yhteys_henkilo_tiedot= null;
    var $promo= null;
    var $kotikalenteri_id= null;
    var $liitetiedosto= null;
 
    function __construct( &$db ) {
        parent::__construct("#__tapahtuma", 'id', $db);
    }
}



class EventcalendarModelGeorss extends JModel {
    /**
	 * Constructor
	 */
	function __construct() {
		parent::__construct();
		$this->alustaDb();
    }
	
		/* Haetaan tapahtumat */
	var $_eventData;
	
	function alustaDb()
	{
		$joku = new DatabaseHelper();
		$option = $joku->get('option');
		
		$db = & JDatabase::getInstance( $option );
		$this->setDBO($db);	
	}
	
	// Alkuaika: pp.kk.vvvv
	function getEvents($alkuaika = null, $limit = 4)
	{
		if($alkuaika == null)
		{
			date_default_timezone_set('Europe/Helsinki');
           	$alkuaika = date('Y-m-d');
		}else{
			muotoilePvmDb(&$alkuaika);
		}

		//$kalenteri_id = $this->getCurrentCalendar();
		$kalenteri_id = getCurrentCalendar();
		
		if(empty($this->_eventData)) 
		{
			$limit = intval($limit);
			if($limit > 0){$limitsql = "LIMIT $limit";}
			//Haetaan tapahtumat valitusta kalenterista
			$query = "SELECT * FROM ".getGlobalDbPrefix()."tapahtuma where 
			((alkuaika >= '$alkuaika') OR (alkuaika < '$alkuaika' AND loppuaika >= '$alkuaika'))
			AND ID IN (SELECT TAPAHTUMA_ID FROM ".getGlobalDbPrefix()."kalenteri_has_tapahtuma where KALENTERI_ID =".$kalenteri_id. 
			" AND TILA = 1 AND EHDOTETTU = 0 ) ORDER BY alkuaika ASC $limitsql";
			
    		$this->_eventData = $this->_getList($query);
			return $this->_eventData;
    	}

		//$this->_eventData = $this->_getList($query);
		//return $this->_eventData;	
		
	}
	
	function getPosition($pos_id){
		$query = "SELECT * FROM ".getPositionDbPrefix()."dpposition where dpposition_id= ".(int) $pos_id;
		return $this->_getList($query);
	}
	
	
}
